package com.dinian.demeter.service.impl;

import com.dinian.demeter.common.constant.FaultConstant;
import com.dinian.demeter.domain.Fault;
import com.dinian.demeter.mapper.FaultMapper;
import com.dinian.demeter.service.FaultService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FaultServiceImpl implements FaultService {

    @Autowired
    private FaultMapper faultMapper;

    /**
     * 根据UserId查询故障信息
     * @param userId
     * @return
     */
    @Override
    public List<Fault> findFaultByUserId(String userId) {
        return faultMapper.queryFaultByUserId(Long.parseLong(userId));
    }

    /**
     * 根据faultId查询故障信息
     * @param faultId
     * @return
     */
    @Override
    public Fault findFaultById(String faultId) {
        return faultMapper.queryFaultById(Long.parseLong(faultId));
    }

    /**
     * 添加故障信息
     * @param fault
     * @return
     */
    @Override
    public Boolean addFault(Fault fault) {
        log.debug("fault:{}",fault);
        fault.setDealState(FaultConstant.START_DEAL);//新提交报修，状态未处理
        int i = faultMapper.insertFault(fault);
        log.info("insert success i : {} ", i);
        if (i == 1)
            return true;
        return false;
    }

    /**
     * 根据ids删除故障信息
     * @param faultIds
     */
    @Override
    public Boolean removeFault(String faultIds) {
        long[] ids = (long[]) ConvertUtils.convert(faultIds.split(","),long.class);
        try {
            faultMapper.deleteFaultByIds(ids);
            return true;
        }catch (Exception e){
            e.getMessage();
            return false;
        }
    }

    /**
     * 根据id更新故障信息
     * @param fault
     * @return
     */
    @Override
    public Boolean modifyFault(Fault fault) {
        try {
            faultMapper.updateFault(fault);
            return true;
        }catch (Exception e){
            e.getMessage();
            return false;
        }
    }


}
